Ext.define('AM.controller.Test', {
	extend : 'Ext.app.Controller',
	stores : [ 'Users' ],
	models : [ 'User' ],
	views : [ 'test.List', 'test.Edit' ],
	refs:[{
		ref:'testList',
		selector:'testlist'
	},{
		ref:'testGridList',
		selector:'testgridlist'
	},{
		ref:'testQueryPannel',
		selector:'testquerypannel'
	}],
	init : function() {
		this.control(
			{
				'testquerypannel':{
					render: this.queryRender
				},
				'testgridlist' : {
					itemdblclick : this.editUser
				},
				'testlist button[action=addtest]' : {
					click: this.addTest
				},
				'testlist button[action=deltest]':{
					click : this.delTest
				},
				'testlist button[action=edittest]':{
					click : this.editTest
				},
				'testlist button[action=querytest]':{
					click : this.queryTest	
				},
				'testedit button[action=save]':{
					click : this.updateTest
				}
		});
	},
	queryRender : function() {
		var queryPanel = this.getTestQueryPannel();
		queryPanel.add({
			layout : 'hbox',
			margin : '0 0 10 0 ',
			defaults : {
				labelAlign : 'right',
				labelWidth : 50,
			},
			items : [ {
				xtype : 'numberfield',
				name : 'name1',
				fieldLabel : 'Name1',
				hideTrigger:true
			}, {
				xtype : 'textfield',
				name : 'name2',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name3',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name4',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name5',
				fieldLabel : 'Name2',
			} ]
		});
		queryPanel.add({
			layout : 'hbox',
			defaults : {
				labelWidth : 50,
				labelAlign : 'right'
			},
			items : [ {
				xtype : 'textfield',
				name : 'name6',
				fieldLabel : 'Name1',
			}, {
				xtype : 'textfield',
				name : 'name7',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name8',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name19',
				fieldLabel : 'Name2',
			}, {
				xtype : 'textfield',
				name : 'name0',
				fieldLabel : 'Name2',
			} ]
		} );
	},
	queryTest : function() {
		this.getUsersStore().remoteFilter = true;
		this.getUsersStore().clearFilter(true);
		var filters = new Array();
		if (!this.getTestQueryPannel().getForm().isValid()) {
			return;
		}
		var values = this.getTestQueryPannel().getForm().getValues(false,true,true,true);
		Ext.Object.each(values, function(key, value, myself) {
		    var filter = Ext.util.Filter.create({
		    	property: key,
		    	value:value
		    });
		    filters.push(filter);
		});
		
		if (filters.length > 0) {
			console.log(filters);
			this.getUsersStore().filter(filters);
		}
	},
	editTest : function() {
		console.log('edit');
		var testList = this.getTestGridList();
		
		var selectModel = testList.getSelectionModel();
		
		if (!selectModel.hasSelection()) {
			console.log('no select');
			return;
		}
		
		var record = testList.getSelectionModel().getSelection()[0];
		this.editUser(testList, record);
	},
	editUser : function(grid, record) {
		var view = Ext.widget('testedit');
		view.down('form').loadRecord(record);
	},
	addTest: function() {
		var view = Ext.widget('testedit');
		var record = this.getUserModel().create();
		view.down('form').loadRecord(record);
	},
	delTest: function() {
		var testList = this.getTestGridList();
		
		var selectModel = testList.getSelectionModel();
		
		if (!selectModel.hasSelection()) {
			console.log('no select');
			return;
		}
		
		var record = testList.getSelectionModel().getSelection()[0];
		this.getUsersStore().remove(record);
		this.getUsersStore().sync();
	},
	updateTest : function(button) {
				var win = button.up('window'), 
				form = win.down('form'), 
				record = form.getRecord(), 
				values = form.getValues();
				record.set(values);
				win.close();
				this.getUsersStore().add(record);
				this.getUsersStore().sync();
	}
});